package com.me.modules.demo.controller;

import com.me.modules.activiti.dto.ProcessTaskDto;
import com.me.modules.aff.entity.AffairsInfoEntity;
import com.me.modules.aff.service.AffairsInfoService;
import com.me.modules.assign.entity.TskAssignInfoEntity;
import com.me.modules.assign.service.TskAssignInfoService;
import com.me.modules.chg.entity.ChangeInfoEntity;
import com.me.modules.chg.service.ChangeInfoService;
import com.me.modules.common.utils.JsonUtil;
import com.me.modules.demo.entity.HiLeaveEntity;
import com.me.modules.demo.entity.LeaveEntity;
import com.me.modules.demo.service.HiLeaveService;
import com.me.modules.demo.service.LeaveService;
import com.me.modules.incident.entity.IncidentInfoEntity;
import com.me.modules.incident.service.IncidentInfoService;
import com.me.modules.knowledge.entity.KnowledgeInfoEntity;
import com.me.modules.knowledge.service.KnowledgeInfoService;
import com.me.modules.problem.entity.ProblemInfoEntity;
import com.me.modules.problem.service.ProblemInfoService;
import com.me.modules.publish.entity.ReleaseInfoEntity;
import com.me.modules.publish.service.ReleaseInfoService;
import com.me.modules.srm.entity.SrmInfoEntity;
import com.me.modules.srm.service.SrmInfoService;
import com.me.modules.sys.entity.ExtendField;
import com.me.modules.sys.entity.OperationEntity;
import com.me.modules.sys.entity.ExtendFieldValue;
import com.me.modules.sys.service.ExtendFieldService;
import com.me.modules.sys.service.OperationService;
import com.me.modules.sys.service.ExtendFieldValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;

/**
 * 类的功能描述.
 * 流程相关的业务根据业务id查询公共类，路径为actKey，也就是业务key
 * @Auther me
 * @Date 2018/8/7
 */
@RequestMapping("act/busInfo")
@Controller
public class ActBusInfoController {

    @Autowired
    LeaveService leaveService;
    @Autowired
    HiLeaveService hiLeaveService;
    @Autowired
    ExtendFieldService feldDictionaryService;
    @Autowired
    ExtendFieldValueService valueDictionaryService;
    @Autowired
    private ProblemInfoService problemInfoService;
    @Autowired
    private IncidentInfoService incidentInfoService;

    @Autowired
    private ChangeInfoService changeInfoService;
    @Autowired
    private AffairsInfoService affairsInfoService;
    @Autowired
    private SrmInfoService srmInfoService;
    @Autowired
    private KnowledgeInfoService knowledgeInfoService;
    @Autowired
    private OperationService operationService;
    @Autowired
    private ReleaseInfoService releaseInfoService;
    @Autowired
    private TskAssignInfoService tskAssignInfoService;

    @RequestMapping(value ="leave",method = RequestMethod.POST)
    public String leave(ProcessTaskDto processTaskDto, Model model, String flag){
        LeaveEntity leaveEntity = leaveService.queryObject(processTaskDto.getBusId());
        List<ExtendField> fieldList=feldDictionaryService.queryByTableName("leave");
        if(fieldList.size()>0){
            String tableId=leaveEntity.getId();
            List<ExtendFieldValue> valueList=new ArrayList<ExtendFieldValue>();
            for(int j=0;j<fieldList.size();j++){
                Long fieldID=fieldList.get(j).getId();

                ExtendFieldValue value=valueDictionaryService.findByFieldIdAndUserId(fieldID,tableId);
                if(value!=null){
                    valueList.add(value);
                }else{
                    valueList.add(new ExtendFieldValue());
                }
            }
            leaveEntity.setValueDictionarys(valueList);
        }
        model.addAttribute("fieldList",fieldList);
        model.addAttribute("leave",leaveEntity);
        model.addAttribute("taskDto",processTaskDto);
        model.addAttribute("flag",flag);
        return "/demo/leaveActInfo";
    }


    @RequestMapping(value ="leavehi",method = RequestMethod.POST)
    public String hileave(ProcessTaskDto processTaskDto, Model model, String flag){
        HiLeaveEntity leaveEntity = hiLeaveService.queryObject(processTaskDto.getBusId());
        List<ExtendField> fieldList=feldDictionaryService.queryByTableName("leave");
        if(fieldList.size()>0){
            String tableId=leaveEntity.getId().toString();
            List<ExtendFieldValue> valueList=new ArrayList<ExtendFieldValue>();
            for(int j=0;j<fieldList.size();j++){
                Long fieldID=fieldList.get(j).getId();

                ExtendFieldValue value=valueDictionaryService.findByFieldIdAndUserId(fieldID,tableId);
                if(value!=null){
                    valueList.add(value);
                }else{
                    valueList.add(new ExtendFieldValue());
                }
            }
            leaveEntity.setValueDictionarys(valueList);
        }
        model.addAttribute("fieldList",fieldList);
        model.addAttribute("leave",leaveEntity);
        model.addAttribute("taskDto",processTaskDto);
        model.addAttribute("flag",flag);
        return "/demo/hiLeaveActInfo";
    }

    /**
     * 问题流程测试审批
     *
     * @param processTaskDto
     * @param model
     * @param flag
     * @return
     */
    @RequestMapping(value ="problem",method = RequestMethod.POST)
    public String problem(ProcessTaskDto processTaskDto, Model model, String flag){
        ProblemInfoEntity problemInfoEntity = problemInfoService.queryObject(processTaskDto.getBusId());
        List<ExtendField> fieldList=feldDictionaryService.queryByTableName("problem");
        if(fieldList.size()>0){
            String tableId=problemInfoEntity.getId().toString();
            List<ExtendFieldValue> valueList=new ArrayList<ExtendFieldValue>();
            for(int j=0;j<fieldList.size();j++){
                Long fieldID=fieldList.get(j).getId();

                ExtendFieldValue value=valueDictionaryService.findByFieldIdAndUserId(fieldID,tableId);
                if(value!=null){
                    valueList.add(value);
                }else{
                    valueList.add(new ExtendFieldValue());
                }
            }
            problemInfoEntity.setValueDictionarys(valueList);
        }
        //查询业务系统
        List<OperationEntity> operationList = operationService.queryListByBean(new OperationEntity());
        model.addAttribute("operationTree",JsonUtil.getJsonByObj(operationList));

        model.addAttribute("fieldList",fieldList);
        model.addAttribute("problem",problemInfoEntity);
        model.addAttribute("taskDto",processTaskDto);
        model.addAttribute("flag",flag);
        return "pro/problemActInfo";
    }


    /**
     * 信息
     */
    @RequestMapping(value ="incident",method = RequestMethod.POST)
    public String incident(ProcessTaskDto processTaskDto, Model model, String flag){
        List<ExtendField> fieldList=feldDictionaryService.queryByTableName("incident");
        IncidentInfoEntity incidentInfo = incidentInfoService.queryObject(Long.parseLong(processTaskDto.getBusId()));
        if(fieldList.size()>0){
            String tableId=incidentInfo.getId().toString();
            List<ExtendFieldValue> valueList=new ArrayList<ExtendFieldValue>();
            for(int j=0;j<fieldList.size();j++){
                Long fieldID=fieldList.get(j).getId();

                ExtendFieldValue value=valueDictionaryService.findByFieldIdAndUserId(fieldID,tableId);
                if(value!=null){
                    valueList.add(value);
                }else{
                    valueList.add(new ExtendFieldValue());
                }
            }
            incidentInfo.setValueDictionarys(valueList);
        }
        model.addAttribute("incidentInfo", incidentInfo);
        model.addAttribute("fieldList",fieldList);
        model.addAttribute("taskDto",processTaskDto);
        model.addAttribute("flag",flag);
        //查询业务系统
        List<OperationEntity> operationList = operationService.queryListByBean(new OperationEntity());
        model.addAttribute("operationTree",JsonUtil.getJsonByObj(operationList));
        return "incident/incidentActInfo";

    }


    /**
     * 信息
     */
    @RequestMapping(value ="change",method = RequestMethod.POST)
    public String change(ProcessTaskDto processTaskDto, Model model, String flag){
        List<ExtendField> fieldList=feldDictionaryService.queryByTableName("change");
        ChangeInfoEntity changeInfo = changeInfoService.queryObject(Long.parseLong(processTaskDto.getBusId()));
        if(fieldList.size()>0){
            String tableId=changeInfo.getId().toString();
            List<ExtendFieldValue> valueList=new ArrayList<ExtendFieldValue>();
            for(int j=0;j<fieldList.size();j++){
                Long fieldID=fieldList.get(j).getId();

                ExtendFieldValue value=valueDictionaryService.findByFieldIdAndUserId(fieldID,tableId);
                if(value!=null){
                    valueList.add(value);
                }else{
                    valueList.add(new ExtendFieldValue());
                }
            }
            changeInfo.setValueDictionarys(valueList);
        }
        model.addAttribute("changeInfo", changeInfo);
        model.addAttribute("fieldList",fieldList);
        model.addAttribute("taskDto",processTaskDto);
        model.addAttribute("flag",flag);
        //查询业务系统
        List<OperationEntity> operationList = operationService.queryListByBean(new OperationEntity());
        model.addAttribute("operationTree",JsonUtil.getJsonByObj(operationList));
        return "change/changeActInfo";

    }


    /**
     * 信息
     */
    @RequestMapping(value ="srm",method = RequestMethod.POST)
    public String srm(ProcessTaskDto processTaskDto, Model model, String flag){
        List<ExtendField> fieldList=feldDictionaryService.queryByTableName("srm");
        SrmInfoEntity srmInfo = srmInfoService.queryObject(Long.parseLong(processTaskDto.getBusId()));
        if(fieldList.size()>0){
            String tableId=srmInfo.getId().toString();
            List<ExtendFieldValue> valueList=new ArrayList<ExtendFieldValue>();
            for(int j=0;j<fieldList.size();j++){
                Long fieldID=fieldList.get(j).getId();

                ExtendFieldValue value=valueDictionaryService.findByFieldIdAndUserId(fieldID,tableId);
                if(value!=null){
                    valueList.add(value);
                }else{
                    valueList.add(new ExtendFieldValue());
                }
            }
            srmInfo.setValueDictionarys(valueList);
        }
        model.addAttribute("srmInfo", srmInfo);
        model.addAttribute("fieldList",fieldList);
        model.addAttribute("taskDto",processTaskDto);
        model.addAttribute("flag",flag);
        //查询业务系统
        List<OperationEntity> operationList = operationService.queryListByBean(new OperationEntity());
        model.addAttribute("operationTree",JsonUtil.getJsonByObj(operationList));
        return "srm/srmActInfo";

    }
    /**
     * 信息
     */
    @RequestMapping(value ="affairs",method = RequestMethod.POST)
    public String affairs(ProcessTaskDto processTaskDto, Model model, String flag){
        List<ExtendField> fieldList=feldDictionaryService.queryByTableName("affairs");
        AffairsInfoEntity affairsInfo = affairsInfoService.queryObject(Long.parseLong(processTaskDto.getBusId()));
        if(fieldList.size()>0){
            String tableId=affairsInfo.getId().toString();
            List<ExtendFieldValue> valueList=new ArrayList<ExtendFieldValue>();
            for(int j=0;j<fieldList.size();j++){
                Long fieldID=fieldList.get(j).getId();

                ExtendFieldValue value=valueDictionaryService.findByFieldIdAndUserId(fieldID,tableId);
                if(value!=null){
                    valueList.add(value);
                }else{
                    valueList.add(new ExtendFieldValue());
                }
            }
            affairsInfo.setValueDictionarys(valueList);
        }
        model.addAttribute("affairsInfo", affairsInfo);
        model.addAttribute("fieldList",fieldList);
        model.addAttribute("taskDto",processTaskDto);
        model.addAttribute("flag",flag);
        //查询业务系统
        List<OperationEntity> operationList = operationService.queryListByBean(new OperationEntity());
        model.addAttribute("operationTree",JsonUtil.getJsonByObj(operationList));
        return "aff/affairsActInfo";

    }
    /**
     * 知识流程测试审批
     *
     * @param processTaskDto
     * @param model
     * @param flag
     * @return
     */
    @RequestMapping(value ="knowledge",method = RequestMethod.POST)
    public String knowledge(ProcessTaskDto processTaskDto, Model model, String flag){
        KnowledgeInfoEntity knowledgeInfoEntity = knowledgeInfoService.queryObject(processTaskDto.getBusId());
        List<ExtendField> fieldList=feldDictionaryService.queryByTableName("knowledge");
        if(fieldList.size()>0){
            String tableId=knowledgeInfoEntity.getId().toString();
            List<ExtendFieldValue> valueList=new ArrayList<>();
            for(int j=0;j<fieldList.size();j++){
                Long fieldID=fieldList.get(j).getId();

                ExtendFieldValue value=valueDictionaryService.findByFieldIdAndUserId(fieldID,tableId);
                if(value!=null){
                    valueList.add(value);
                }else{
                    valueList.add(new ExtendFieldValue());
                }
            }
            knowledgeInfoEntity.setValueDictionarys(valueList);
        }
        model.addAttribute("fieldList",fieldList);
        model.addAttribute("knowledge",knowledgeInfoEntity);
        model.addAttribute("taskDto",processTaskDto);
        model.addAttribute("flag",flag);
        //查询业务系统
        List<OperationEntity> operationList = operationService.queryListByBean(new OperationEntity());
        model.addAttribute("operationTree",JsonUtil.getJsonByObj(operationList));
        return "knowledge/knowledgeActInfo";
    }


    /**
     * 发布流程审批
     * @param processTaskDto
     * @param model
     * @param flag
     * @return
     */
    @RequestMapping(value ="release",method = RequestMethod.POST)
    public String release(ProcessTaskDto processTaskDto, Model model, String flag){
        ReleaseInfoEntity releaseInfoEntity = releaseInfoService.queryObject(Long.valueOf(processTaskDto.getBusId()));
        List<ExtendField> fieldList=feldDictionaryService.queryByTableName("release");
        if(fieldList.size()>0){
            String tableId=releaseInfoEntity.getId().toString();
            List<ExtendFieldValue> valueList=new ArrayList<>();
            for(int j=0;j<fieldList.size();j++){
                Long fieldID=fieldList.get(j).getId();
                ExtendFieldValue value=valueDictionaryService.findByFieldIdAndUserId(fieldID,tableId);
                if(value!=null){
                    valueList.add(value);
                }else{
                    valueList.add(new ExtendFieldValue());
                }
            }
            releaseInfoEntity.setValueDictionarys(valueList);
        }
        model.addAttribute("fieldList",fieldList);
        model.addAttribute("taskDto",processTaskDto);
        model.addAttribute("flag",flag);
        model.addAttribute("release",releaseInfoEntity);
        //查询业务系统
        List<OperationEntity> operationList = operationService.queryListByBean(new OperationEntity());
        model.addAttribute("operationTree",JsonUtil.getJsonByObj(operationList));
        return "release/releaseActInfo";
    }


    /**
     * 任务流程审批
     * @param processTaskDto
     * @param model
     * @param flag
     * @return
     */
    @RequestMapping(value ="assign",method = RequestMethod.POST)
    public String assign(ProcessTaskDto processTaskDto, Model model, String flag){
        TskAssignInfoEntity tskAssignInfoEntity = tskAssignInfoService.queryObject(Long.valueOf(processTaskDto.getBusId()));
        List<ExtendField> fieldList=feldDictionaryService.queryByTableName("assign");
        if(fieldList.size()>0){
            String tableId=tskAssignInfoEntity.getId().toString();
            List<ExtendFieldValue> valueList=new ArrayList<>();
            for(int j=0;j<fieldList.size();j++){
                Long fieldID=fieldList.get(j).getId();
                ExtendFieldValue value=valueDictionaryService.findByFieldIdAndUserId(fieldID,tableId);
                if(value!=null){
                    valueList.add(value);
                }else{
                    valueList.add(new ExtendFieldValue());
                }
            }
            tskAssignInfoEntity.setValueDictionarys(valueList);
        }
        model.addAttribute("fieldList",fieldList);
        model.addAttribute("taskDto",processTaskDto);
        model.addAttribute("flag",flag);
        model.addAttribute("assign",tskAssignInfoEntity);
        //查询业务系统
        List<OperationEntity> operationList = operationService.queryListByBean(new OperationEntity());
        model.addAttribute("operationTree",JsonUtil.getJsonByObj(operationList));
        return "assign/assignActInfo";
    }

}
